package com.jinyi.wwdeng.ui.config.act

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.View
import com.jinyi.base.base.BaseActivity
import com.jinyi.base.utils.img.ImageUtil
import com.jinyi.base.utils.thread.TimingThread
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.ActUploadBinding

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/22 19:25
 * @Annotation：图片上传页面
 */
class UploadAct: BaseActivity(),TimingThread.ITimingThreadListener {

    lateinit var binding: ActUploadBinding
    private val KEY_FILE_PATH = "filePath"//文件路径

    var timingThread: TimingThread? = null
    var uploadProcess = 0//上传进度

    override fun getRootView(): View {
        binding = ActUploadBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        setTitleBarVisibility(View.VISIBLE)
        setTextTitle(getString(R.string.uploading))
        setTextTitleGravity(Gravity.CENTER)
        setRightBtnRes(R.mipmap.pic_kefu_black_icon)

        binding.progressBar.max = 100
        binding.progressBar.progress = uploadProcess
        binding.tvProgress.text = "$uploadProcess%"
        showUploadingView()
        startThread()
    }


    override fun initListener() {
        //使用照片
        binding.btnUsePhoto.setOnClickListener {
            showToast(binding.btnUsePhoto.text.toString())
        }

        //重新获取
        binding.btnRetake.setOnClickListener {
            showToast(binding.btnRetake.text.toString())
        }
    }

    override fun initData() {
        val bundle = intent.extras
        val imgPath = bundle!!.getString(KEY_FILE_PATH)
        ImageUtil.getInstance().loadImage(this,binding.ivImage,imgPath)
    }

    /**
     * 上传中状态
     */
    private fun showUploadingView(){
        binding.tvProgress.visibility = View.VISIBLE
        binding.ivConfirm.visibility = View.GONE
        binding.btnRetake.alpha = 0.5f
        binding.btnRetake.isEnabled = false
        binding.btnUsePhoto.alpha = 0.5f
        binding.btnUsePhoto.isEnabled = false
    }

    /**
     * 上传完成
     */
    private fun showUploadCompleteView(){
        binding.tvProgress.visibility = View.GONE
        binding.ivConfirm.visibility = View.VISIBLE
        binding.btnRetake.alpha = 1f
        binding.btnRetake.isEnabled = true
        binding.btnUsePhoto.alpha = 1f
        binding.btnUsePhoto.isEnabled = true
    }

    /**
     * 模拟上传
     */
    private fun startThread(){
        if(timingThread == null){
            timingThread = TimingThread()
            timingThread!!.setThreadDelayMillis(1000)
            timingThread!!.setLoadListener(this)
        }
        timingThread!!.startThread()
    }

    override fun onLoading() {
        if (uploadProcess >= 100){
            uploadProcess = 0
            timingThread!!.stopThread()

            showUploadCompleteView()
            return
        }
        uploadProcess++
        binding.progressBar.progress = uploadProcess
        binding.tvProgress.text = "$uploadProcess%"
    }

    override fun onDestroy() {
        super.onDestroy()
        if(timingThread != null){
            timingThread!!.stopThread()
            timingThread = null
        }
    }

}